package com.outsourcing.bottle.ui;

import java.util.HashMap;

import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.outsourcing.bottle.BasicActivity;
import com.outsourcing.bottle.R;
import com.outsourcing.bottle.domain.UrlConfig;
import com.outsourcing.bottle.util.AppContext;
import com.outsourcing.bottle.util.BasicUIEvent;
import com.outsourcing.bottle.util.HttpUtils;
import com.outsourcing.bottle.util.UICore;
/**
 * 瓶友等级
 * @author 06peng
 *
 */
public class BtFriendLevelActivity extends BasicActivity implements Callback, BasicUIEvent, OnClickListener {
	
	private final int get_bt_friend_level_info = 1;
	private final int upgrade_bt_friend_level = 2;
	private final int error = 3;
	
	private ImageView backView;
	private ImageView homeView;
	private Button upgradeBtn;
	private WebView webView;
	private TextView creditView;
	private TextView ttCreditView;
	private TextView levelView;
	
	private Handler handler;
	
	private int credit;
	private int exp;
	private int level;
	private int ttcredit;
	private int ttexp;
	private String level_url;
	private int show_upgrade;
	
	@Override
	protected void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);
		handler = new Handler(this);
		setContentView(R.layout.bt_friend_level);
		initWidget();
		UICore.eventTask(this, this, get_bt_friend_level_info, "", null);
	}
	
	private void initWidget() {
		backView = (ImageView) findViewById(R.id.topbar_back);
		backView.setOnClickListener(this);
		homeView = (ImageView) findViewById(R.id.topbar_home);
		homeView.setOnClickListener(this);
		upgradeBtn = (Button) findViewById(R.id.bt_friend_upgrade);
		upgradeBtn.setOnClickListener(this);
		webView = (WebView) findViewById(R.id.webView);
		creditView = (TextView) findViewById(R.id.bt_friend_level_credit_and_experience_value);
		ttCreditView = (TextView) findViewById(R.id.bt_friend_level_ttcredit_and_ttexperience_value);
		levelView = (TextView) findViewById(R.id.bt_friend_level_value);
	}
	
	private void updateUI() {
		try {
			creditView.setText(credit + "/" + exp);
			ttCreditView.setText(ttcredit + "/" + ttexp);
			levelView.setText(level + "");
			webView.loadUrl(level_url);
			if (show_upgrade == 1) {
				upgradeBtn.setVisibility(View.VISIBLE);
			} else {
				upgradeBtn.setVisibility(View.GONE);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void execute(int mes, Object obj) {
		switch (mes) {
		case get_bt_friend_level_info:
			getBtLevelInfo();
			handler.sendEmptyMessage(get_bt_friend_level_info);
			break;
		case upgrade_bt_friend_level:
			upgradeUserLevel();
			break;
		default:
			break;
		}
	}

	@Override
	public boolean handleMessage(Message msg) {
		switch (msg.what) {
		case get_bt_friend_level_info:
			updateUI();
			break;
		case upgrade_bt_friend_level:
			onToast(msg.obj.toString());
			break;
		case error:
			onToast(msg.obj.toString());
			break;
		default:
			break;
		}
		return false;
	}

	@Override
	public void onClick(View v) {
		if (v == backView) {
			finish();
		} else if (v == homeView) {
			Intent intent = new Intent(this, HomeActivity.class);
			intent.putExtra("currentItem", 1);
			startActivity(intent);
		} else if (v == upgradeBtn) {
			UICore.eventTask(this, this, upgrade_bt_friend_level, "", null);
		}
	}

	private void getBtLevelInfo() {
		StringBuffer url = new StringBuffer(UrlConfig.get_user_btlevel);
		url.append("?clitype=2");
		url.append("&language=" + AppContext.language);
		url.append("&login_uid=" + AppContext.getInstance().getLogin_uid());
		url.append("&login_token=" + AppContext.getInstance().getLogin_token());
		try {
			String result = HttpUtils.doGet(url.toString());
			if (result == null) {
				return;
			}
			JSONObject obj = new JSONObject(result);
			JSONObject resultobj = obj.getJSONObject("user_btlevel");
			credit = resultobj.getInt("credit");
			exp = resultobj.getInt("exp");
			level = resultobj.getInt("level");
			level_url = resultobj.getString("level_url");
			show_upgrade = resultobj.getInt("show_upgrade");
			ttcredit = resultobj.getInt("ttcredit");
			ttexp = resultobj.getInt("ttexp");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void upgradeUserLevel() {
		String url = UrlConfig.btlevel_upgrade;
		HashMap<String, String> paramsMap = new HashMap<String, String>();
		paramsMap.put("login_uid", String.valueOf(AppContext.getInstance().getLogin_uid()));
		paramsMap.put("login_token", AppContext.getInstance().getLogin_token());
		try {
			String result = HttpUtils.doPost(this, url, paramsMap);
			if (result != null) {
	        	JSONObject object = new JSONObject(result);
	        	JSONObject resultObj = object.getJSONObject("results");
	        	int success = resultObj.getInt("success");
	        	if (success == 0) {
	        		String errmsg = resultObj.getString("errmsg");
	        		Message msg = Message.obtain(handler, error, errmsg);
	        		handler.sendMessage(msg);
	        	} else {
	        		String successmsg = resultObj.getString("successmsg");
					Message msg = Message.obtain(handler, upgrade_bt_friend_level, successmsg);
					handler.sendMessage(msg);
	        	}
	        }
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
